package com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.miniapp;

import cn.hutool.core.net.URLDecoder;
import com.allinpay.sdk.bean.BizParameter;
import com.xiaoshuidi.cloud.framework.pay.core.client.AbstractPayCodeMapping;
import com.xiaoshuidi.cloud.framework.pay.core.client.PayCommonResult;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayCloseUnifiedReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayNotifyDataDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayOrderNotifyRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayQueryUnifiedReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayQueryUnifiedRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayRefundNotifyDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayRefundUnifiedReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.PayRefundUnifiedRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.AbstractTongshangyunPayClient;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.TongshangyunClientConfig;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.enums.TongshangyunApiType;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 通商云小程勋支付接口抽象类
 */
@Slf4j
public abstract class AbstractTongshangyunMiniAppPayClient extends AbstractTongshangyunPayClient {
    public AbstractTongshangyunMiniAppPayClient(Long channelId, String channelCode, TongshangyunClientConfig config, AbstractPayCodeMapping codeMapping) {
        super(channelId, channelCode, config, codeMapping);
    }

    @Override
    protected PayCommonResult<PayRefundUnifiedRespDTO> doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) throws Throwable {
        return null;
    }

    @Override
    protected PayCommonResult<?> doUnifiedClose(PayCloseUnifiedReqDTO reqDTO) throws Throwable {
        return null;
    }


    @Override
    public PayOrderNotifyRespDTO parseOrderNotify(PayNotifyDataDTO data) throws Exception {
        //进行验签
        Map<String, String> params = data.getParams();
        String sign = params.get("sign");
        String str = "appId=".concat(params.get("appId")).concat("&bizContent=").concat(params.get("bizContent"))
                .concat("&charset=").concat(params.get("charset")).concat("&notifyId=").concat(params.get("notifyId"))
                .concat("&notifyTime=").concat(params.get("notifyTime")).concat("&notifyType=").concat(params.get("notifyType"))
                .concat("&version=").concat(params.get("version"));
        String signValue = URLDecoder.decode(str, StandardCharsets.UTF_8);
        return PayOrderNotifyRespDTO.builder().success(client.checkSign(signValue, sign)).build();
    }

    @Override
    public PayRefundNotifyDTO parseRefundNotify(PayNotifyDataDTO notifyData) {
        return null;
    }

    @Override
    public <T> PayCommonResult<PayQueryUnifiedRespDTO<T>> unifiedQuery(PayQueryUnifiedReqDTO reqDTO) {
        BizParameter param = new BizParameter();
        param.put("bizOrderNo", reqDTO.getMerchantOrderId());
        return call(TongshangyunApiType.GET_ORDER_DETAIL.getApi(), param, reqDTO.getMerchantOrderId());
    }


}
